# -*- coding: utf-8 -*-

from ir.ir_helper import Helper


def start_local_var(ticket: str, vars: dict, helper: Helper):
    if ticket in helper.tickets:
        raise Exception('ticket {} already opens.'.format(ticket))
    # Save the ticket.
    helper.tickets[ticket] = helper.local
    # Prepare the local temperary variable dictionary.
    current = helper.local.copy()
    current.update(vars)
    # Set current as new local var.
    helper.local = current


def end_local_var(ticket: str, helper: Helper):
    # Check in the current.
    if ticket not in helper.tickets:
        return
    # Replace the current to that ticket.
    helper.local = helper.tickets[ticket]
    # Delete that ticket.
    del helper.tickets[ticket]